package com.hh.bss.project.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hh.bss.base.BaseBusinessService;
import com.hh.bss.base.EntityDao;
import com.hh.bss.common.page.Page;
import com.hh.bss.project.dao.AdvancePaymentDao;
import com.hh.bss.project.entity.AccountPn;
import com.hh.bss.project.entity.AdvancePayment;
import com.hh.bss.project.service.AccountPnService;
import com.hh.bss.project.service.AdvancePaymentService;
import com.hh.bss.project.vo.query.AccountPnQuery;
import com.hh.bss.project.vo.query.AdvancePaymentQuery;
import com.hh.bss.util.extjs.JsonHelper;

@Service
@Transactional
public class AdvancePaymentServiceImpl extends  BaseBusinessService<AdvancePayment, Integer> implements AdvancePaymentService<AdvancePayment, Integer> {

	@Autowired 
    private AdvancePaymentDao advancePaymentDao;
	
	@Autowired
	private AccountPnService accountPnService;
    
	@Override
	public EntityDao getEntityDao() {
		return this.advancePaymentDao;
	}
	
	@Override
	@Transactional(readOnly=true)
	public Page findPage(AdvancePaymentQuery query) {
		return advancePaymentDao.findPage(query);
	}

	@Override
	public void save(AdvancePayment entity) throws DataAccessException {
		List list = accountPnService.findAccountPnListByNbr(entity.getNbr());
		if(list != null && list.size() ==0){
			AccountPn accountPn = new AccountPn();
			accountPn.setFinalAr(entity.getAmount());
			accountPn.setCurrency(entity.getCurrency());
			accountPn.setCustId(entity.getCustId());
			accountPn.setType(2);  //预付款账单
			accountPn.setNbr(entity.getNbr());
			accountPn.setStatus(0); //发送状态 正常
			accountPn.setBranchNbr(entity.getBranchNbr());
			accountPn.setCreator(entity.getCreator());
			accountPn.setCreateDt(new Date());
			entity.setBalance(calculateBalance(entity));
			this.accountPnService.save(accountPn);
			//entity.setBalance(entity.getAmount()+entity.getInAmount()-entity.getOutAmount()-entity.getDeAmount());
			this.advancePaymentDao.save(entity);
			this.advancePaymentDao.flush();
		}
		
	}
	
   @Override
   public void update(AdvancePayment entity) throws DataAccessException {
	   entity.setBalance(calculateBalance(entity));
	   this.advancePaymentDao.update(entity);
	   this.advancePaymentDao.flush();
	   
   };
	
	private Double calculateBalance(AdvancePayment aPayment) {
		double balance = 0D;
		if (aPayment != null) {
			balance = aPayment.getAmount();
			if (aPayment.getInAmount() != null) {
				balance += aPayment.getInAmount();
			}
			if (aPayment.getOutAmount() != null) {
				balance -= aPayment.getOutAmount();
			}
			if (aPayment.getDeAmount() != null) {
				balance -= aPayment.getDeAmount();
			}
		}
		return balance;
	}


}
